#!/bin/sh
# Pour supprimer des gens dans Balloon proprement ...

SQL_USER="root"

## Ne pas editer sous cette ligne ##

SQL_SCRIPT="" 
export SQL_SCRIPT

function add_sql() {
	SQL_SCRIPT="$SQL_SCRIPT\n$1"
}

function add_sql_info() {
	add_sql "SELECT \"## $1\" as \"## INFO ##\"; "
}

function sql_check() {
	SQL_SCRIPT=""
	add_sql_info "begining sql script ..."
	
	add_sql_info "Gonna look inside [member]"
	add_sql "SELECT id, last_name as name, first_name as firstName "
	add_sql "  FROM member WHERE id = $1; "
	
	
	add_sql_info "Gonna look inside [matche_goal_lnk]"
	add_sql "SELECT "
	add_sql "  id, matche_id as matche, member_id as member, helper_id as helper"
	add_sql "  FROM matche_goal_lnk"
	add_sql "  WHERE member_id = $1 OR helper_id = $1; "
	
	add_sql_info "Gonna look inside [matche_member_lnk]"
	add_sql "SELECT id,matche_id as matche,member_id as member"
	add_sql "  FROM matche_member_lnk"
	add_sql "  WHERE member_id = $1; "
	
	add_sql_info "Gonna look inside [matche_warning_lnk]"
	add_sql "SELECT id,matche_id as matche, member_id as member"
	add_sql "  FROM matche_warning_lnk"
	add_sql "  WHERE member_id = $1; "
	
	add_sql_info "Gonna look inside [member_club_function_lnk]"
	add_sql "SELECT id, season_id, member_id, club_function_id "
	add_sql "  FROM member_club_function_lnk "
	add_sql "  WHERE member_id = $1; "
	
	add_sql_info "Gonna look inside [member_cotisation_lnk]"
	add_sql "SELECT id, season_id, member_id, cotisation_id "
	add_sql "  FROM member_cotisation_lnk WHERE member_id = $1; "
	
	add_sql_info "Gonna look inside [team_category]"
	add_sql "SELECT id, label, director_id FROM team_category "
	add_sql "  WHERE director_id = $1; "
	
	add_sql_info "Gonna look inside [team]"
	add_sql "SELECT id, name, coach FROM team WHERE coach = $1 "
	add_sql "  OR director_1 = $1 OR director_2 = $1; "
	
	add_sql_info "Gonna look inside [team_member_lnk]"
	add_sql "SELECT id, team_id, member_id  FROM team_member_lnk "
	add_sql "  WHERE member_id = $1; "
	return 0
}


function sql_delete() {
	SQL_SCRIPT=""
	add_sql_info "Member $1"
	add_sql "SELECT id, last_name as name, first_name as firstName "
	add_sql "  FROM member WHERE id = $1; "
	add_sql_info "Deletion"
	add_sql "DELETE FROM member where id = $1;"
	add_sql "DELETE FROM member_club_function_lnk where member_id = $1;"
	add_sql "DELETE FROM member_cotisation_lnk WHERE member_id = $1;"
	add_sql "DELETE FROM matche_goal_lnk WHERE member_id = $1 OR helper_id = $1;"
	add_sql "DELETE FROM matche_member_lnk WHERE member_id = $1;"

}

function verify() {
	if [ ! $1 ]
		then
			echo "## Error : you must specify a MemberId"
			exit 1
	fi	
	sql_check $1
	echo "## Looking for invalid reference on Member #$1"
	echo "## Using [$SQL_USER] identity to talk with MySql : "
	echo -e $SQL_SCRIPT | mysql -u $SQL_USER -p balloon 
	exit 0
}

function delete() {
	if [ ! $1 ]
		then
			echo "## Error : you must specify a MemberId"
			exit 1
	fi	
	sql_delete $1
	echo "## Deleting Member #$1"
	echo "## Using [$SQL_USER] identity to talk with MySql : "
	echo -e $SQL_SCRIPT | mysql -u $SQL_USER -p balloon 
	exit 0
}

function usage() {
	echo "## ./del_member.sh : "
	echo "##    - verify ID : check ID for safe deletion"
	echo "##    - delete ID : do safe deletion of ID"
	echo "####"
	exit 1
}

main() {
	echo "####"
	echo "## Balloon Deletion Admin Tool"
	echo "####"	
	case $1 in
		"verify") verify $2;;
		"delete") delete $2;;
		*) usage;;
	esac
}


main $@